﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.produceDAL.Stock;
using dhDataAcess.dataAcess.stockDataSetTableAdapters;
using dhDataAcess.dataAcess.baobiaoDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.produceLinq.StockLinq;

namespace dhSystemBLL.produceBLL
{
   public  class materialStockListBLL
    {
       private V_materialStockListTableAdapter _V_materialStockListAdapter = null;
       private V_materialStockListTableAdapter V_materialStockListAdapter
        {
            get
            {
                if (_V_materialStockListAdapter == null) _V_materialStockListAdapter = new V_materialStockListTableAdapter();
                return _V_materialStockListAdapter;

            }
        }
       private materialStockListTableAdapter  _materialStockListAdapter = null;
       private materialStockListTableAdapter materialStockListAdapter
        {
            get
            {
                if (_materialStockListAdapter == null) _materialStockListAdapter = new materialStockListTableAdapter();
                return _materialStockListAdapter;

            }
        }

        /// <summary>
        /// 查询库存周期表
        /// </summary>
        /// <returns></returns>

       public stockDataSet.materialStockListDataTable getmaterialStockListAll()
        {
            try
            {
                return materialStockListAdapter.GetDatamaterialStockListAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 查询库存周期表
       /// </summary>
       /// <returns></returns>

       public baobiaoDataSet.V_materialStockListDataTable getV_materialStockListAll()
       {
           try
           {
               return V_materialStockListAdapter.GetDataV_materialStockListALL ();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 查询原材料库存表
       /// </summary>
       /// <returns></returns>

       public baobiaoDataSet.V_materialStockListDataTable getV_materialStockListBymtypeNoAll()
       {
           try
           {
               return V_materialStockListAdapter.GetDataV_materialStockListBymtypeNo ();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 查询五金库存表
       /// </summary>
       /// <returns></returns>

       public baobiaoDataSet.V_materialStockListDataTable getV_materialStockListBymtypeAll()
       {
           try
           {
               return V_materialStockListAdapter.GetDataV_materialStockListBymtype ();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        /// 根据库存周期查询库存周期表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public stockDataSet.materialStockListDataTable getmaterialStockListAllBymatNum(string matNum)
        {
            try
            {
                return materialStockListAdapter.GetDatamaterialStockListBymatNum(matNum);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 插入信息
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int stockDateInsert(stockDateModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockDateDAL stockDate = new stockDateDAL(tn, conn);
                stockDate.stockDateInsert(row);
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个生产单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int stockDateUpdate(stockDateModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockDateDAL stockDate = new stockDateDAL(tn, conn);
                stockDate.stockDateUpdate(row);
                tn.Commit();
                tn.Commit();
                return i;

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

        public int stockDateDel(string stockDateAccount)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockDateDAL stockDate = new stockDateDAL(tn, conn);
                stockDate.stockDateDel(stockDateAccount);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
